﻿using Basic.FormDAL.Model;
using Basic.FormModel.DB;
using Basic.FormModel.Model;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class PhysicalQueryColumnDAL : BasicDAL<DBPhysicalQueryColumn, long>, IPhysicalQueryColumnDAL
    {
        public PhysicalQueryColumnDAL ( IRepository<DBPhysicalQueryColumn> basicDAL ) : base(basicDAL, "physical.query.column")
        {
        }
        public ColumnSort[] GetSorts ( long tableId, long groupId )
        {
            ISugarQueryable<ColumnSort>[] tables = new ISugarQueryable<ColumnSort>[]
            {
                this._BasicDAL.Queryable.Where(a=>a.TableId==tableId && a.GroupId==groupId).Select(a=>new ColumnSort
                {
                    Id = a.Id,
                    IsGroup=false,
                    Sort=a.Sort
                }),
                this._BasicDAL.Provider.Queryable<DBPhysicalQueryGroup>().Where(a=>a.TableId==tableId && a.ParentId==groupId).Select(a=>new ColumnSort
                {
                    Id = a.Id,
                    IsGroup=true,
                    Sort=a.Sort
                })
            };
            return this._BasicDAL.Provider.UnionAll<ColumnSort>(tables).OrderBy(a => a.Sort).ToArray();
        }
        public long Add ( DBPhysicalQueryColumn add )
        {
            add.Id = IdentityHelper.CreateId();
            this._BasicDAL.Insert(add);
            return add.Id;
        }
        public void SetSort ( KeyValuePair<long, int>[] sort )
        {
            ISqlQueue<DBPhysicalQueryColumn> queue = this._BasicDAL.BeginQueue();
            sort.ForEach(c =>
            {
                queue.UpdateOneColumn(a => a.Sort == c.Value, a => a.Id == c.Key);
            });
            _ = queue.Submit();
        }
        public void SetSort ( ColumnSort sour, ColumnSort to )
        {
            ISqlQueue<DBPhysicalQueryColumn> queue = this._BasicDAL.BeginQueue();
            if ( sour.IsGroup )
            {
                queue.UpdateByOneColumn<DBPhysicalQueryGroup>(a => a.Sort == sour.Sort, a => a.Id == sour.Id);
            }
            else
            {
                queue.UpdateOneColumn(a => a.Sort == sour.Sort, a => a.Id == sour.Id);
            }
            if ( to.IsGroup )
            {
                queue.UpdateByOneColumn<DBPhysicalQueryGroup>(a => a.Sort == to.Sort, a => a.Id == to.Id);
            }
            else
            {
                queue.UpdateOneColumn(a => a.Sort == to.Sort, a => a.Id == to.Id);
            }
            _ = queue.Submit();
        }
        public int GetMaxSort ( long tableId, long groupId )
        {
            ISugarQueryable<MaxSortDto>[] tables = new ISugarQueryable<MaxSortDto>[]
            {
                this._BasicDAL.Queryable.Where(a=>a.TableId==tableId && a.GroupId==groupId && a.IsSystem == false).Select(a=>new MaxSortDto
                {
                    Sort=a.Sort
                }),
                this._BasicDAL.Provider.Queryable<DBPhysicalQueryGroup>().Where(a=>a.TableId==tableId && a.ParentId==groupId).Select(a=>new MaxSortDto
                {
                    Sort=a.Sort
                })
            };
            return this._BasicDAL.Provider.UnionAll<MaxSortDto>(tables).Max(a => a.Sort);
        }
    }
}
